package com.vm.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.model.Comment;
import com.vm.myview.ViewComment;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.Toast;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月31日 下午5:22:37]
 * @Version: [v1.0]
 * 
 */
public class CommentActivity extends Activity {

	Button btn_publish;
	String softId;
	RequestQueue mQueue;
	ViewComment viewComment;

	RatingBar ratingBar;
	EditText et_content;
	LinearLayout found_ll_1;
	Dialog dialog;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comments);

		// softId = getIntent().getStringExtra("softId");
		mQueue = MyVolley.getRequestQueue();
		// viewComment = new ViewComment(this, this);

		// found_ll_1 = (LinearLayout) findViewById(R.id.found_ll_1);
		//
		// init();
		// initData();
	}

	public void initData() {
		found_ll_1.removeAllViews();
		String url = VMConst.REQUEST_URL + "getComment?softId=" + softId;
		JsonArrayRequest commentsArrayRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {

					@Override
					public void onResponse(JSONArray response) {
						for (int i = 0; i < response.length(); i++) {
							try {
								JSONObject jsonObject = response
										.getJSONObject(i);
								Comment comment = JsonUtils.jsonToBean(
										jsonObject, Comment.class);
								if (comment != null) {
									found_ll_1.addView(viewComment.dyAddView(i,
											comment));
								}

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
					}
				});
		mQueue.add(commentsArrayRequest);
	}

	public void init() {
		btn_publish = (Button) findViewById(R.id.btn_publish);
		btn_publish.setOnClickListener(btn_publishListener);
	}

	OnClickListener btn_publishListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			String url = VMConst.REQUEST_URL + "ifPublishComment?userId="
					+ CurrentUser.USERID + "&softId=" + softId;
			StringRequest addCommentRequest = new StringRequest(url,
					new Listener<String>() {

						@Override
						public void onResponse(String response) {
							if (response.contains("1")) {

								Toast toast = Toast.makeText(
										CommentActivity.this, "不能重复评论",
										Toast.LENGTH_SHORT);
								toast.show();
							} else {
								openDialog();
							}
						}
					}, new ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError error) {
							Toast toast = Toast.makeText(CommentActivity.this,
									"提交失败", Toast.LENGTH_SHORT);
							toast.show();
						}
					});

			mQueue.add(addCommentRequest);

		}
	};

	private void openDialog() {
		// View menuView = null;
		View menuView = View.inflate(this, R.layout.published_comments, null);

		dialog = new Dialog(CommentActivity.this, R.style.dialog);

		dialog.setContentView(menuView);
		dialog.show();

		ratingBar = (RatingBar) menuView.findViewById(R.id.ratingBar1);
		et_content = (EditText) menuView.findViewById(R.id.et_comments);
		Button btn_cancel = (Button) menuView.findViewById(R.id.btn_cancel);
		btn_cancel.setOnClickListener(menuClickListener);
		Button btn_confirm = (Button) menuView.findViewById(R.id.btn_confirm);
		btn_confirm.setOnClickListener(menuClickListener);

	}

	OnClickListener menuClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_confirm:
				float fstar = ratingBar.getRating() * 2;

				String star = Integer.toString((int) fstar);
				String content = et_content.getText().toString();
				try {
					content = URLEncoder.encode(content, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String url = VMConst.REQUEST_URL + "addComment?userId="
						+ CurrentUser.USERID + "&username="
						+ CurrentUser.USERNAME + "&softId=" + softId
						+ "&content=" + content + "&star=" + star;
				StringRequest addCommentRequest = new StringRequest(url,
						new Listener<String>() {

							@Override
							public void onResponse(String response) {
								if (response.contains("1")) {
									dialog.cancel();
									Toast toast = Toast.makeText(
											CommentActivity.this, "提交成功",
											Toast.LENGTH_SHORT);
									toast.show();
									initData();
								} else {
									dialog.cancel();
									Toast toast = Toast.makeText(
											CommentActivity.this, "提交失败",
											Toast.LENGTH_SHORT);
									toast.show();

								}
							}
						}, new ErrorListener() {

							@Override
							public void onErrorResponse(VolleyError error) {
								Toast toast = Toast.makeText(
										CommentActivity.this, "提交失败",
										Toast.LENGTH_SHORT);
								toast.show();
							}
						});

				mQueue.add(addCommentRequest);

				break;
			case R.id.btn_cancel:
				dialog.cancel();
				break;

			default:
				break;
			}
		}
	};
}
